package org.example.util;


import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.List;

public class FileUtils {

    // 创建目录
    public static boolean createDirectory(String path) {
        File dir = new File(path);
        return dir.mkdirs();
    }

    // 检查文件是否存在
    public static boolean fileExists(String path) {
        File file = new File(path);
        return file.exists() && file.isFile();
    }

    // 检查目录是否存在
    public static boolean directoryExists(String path) {
        File dir = new File(path);
        return dir.exists() && dir.isDirectory();
    }

    // 选择文件夹目录
    public static String selectDirectory() {
        // 在实际项目中，这里会实现文件选择器逻辑
        // 这里只是演示
        return System.getProperty("user.dir") + "/experiment_reports";
    }

    // 获取目录下所有文件
    public static List<File> getFilesInDirectory(String directoryPath) {
        File directory = new File(directoryPath);
        List<File> files = new ArrayList<>();

        if (directory.exists() && directory.isDirectory()) {
            File[] fileList = directory.listFiles();
            if (fileList != null) {
                for (File file : fileList) {
                    if (file.isFile()) {
                        files.add(file);
                    }
                }
            }
        }

        return files;
    }

    // 复制文件
    public static boolean copyFile(String sourcePath, String destinationPath) {
        Path source = Paths.get(sourcePath);
        Path destination = Paths.get(destinationPath);

        try {
            Files.copy(source, destination, StandardCopyOption.REPLACE_EXISTING);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    // 移动文件
    public static boolean moveFile(String sourcePath, String destinationPath) {
        Path source = Paths.get(sourcePath);
        Path destination = Paths.get(destinationPath);

        try {
            Files.move(source, destination, StandardCopyOption.REPLACE_EXISTING);
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }
}
